package work.mediway.mdm.biz.service.biz.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import work.mediway.mdm.core.enums.StatusWhetherEnum;
import work.mediway.mdm.core.util.DUtil;
import work.mediway.mdm.core.vo.TableVO;
import work.mediway.mdm.biz.service.biz.SsRoleLinkFieldService;
import work.mediway.mdm.core.constant.MdmConstant;
import work.mediway.mdm.biz.entity.biz.SsRoleLinkField;
import work.mediway.mdm.biz.entity.biz.SsRoleLinkTable;
import work.mediway.mdm.biz.helper.BusinessDatabaseHelper;
import work.mediway.mdm.biz.mapper.biz.SsRoleLinkTableMapper;
import work.mediway.mdm.biz.service.biz.SsRoleLinkTableService;
import work.mediway.mdm.biz.vo.dict.DictFieldVO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2022/7/8
 */
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class SsRoleLinkTableServiceImpl extends ServiceImpl<SsRoleLinkTableMapper, SsRoleLinkTable> implements SsRoleLinkTableService {

    private final JdbcTemplate jdbcTemplate;
    private final BusinessDatabaseHelper businessDatabaseHelper;
    @Resource
    private SsRoleLinkFieldService roleLinkFieldService;

    @Override
    public void autoAddPermForAdmin(TableVO tableVO) {
        // 管理员用户每张表自动分配所有权限
        List<String> sysRoles = jdbcTemplate.queryForList("SELECT id FROM t_perm_role WHERE t_perm_role.name LIKE '%管理员%'", String.class);
        if (CollUtil.isNotEmpty(sysRoles)) {
            for (String role : sysRoles) {
                SsRoleLinkTable ssRoleLinkTable = new SsRoleLinkTable();
                ssRoleLinkTable.setSsTable(tableVO.getTableCode());
                ssRoleLinkTable.setSsRole(role);
                ssRoleLinkTable.setSsBrowse(StatusWhetherEnum.STATUS_Y.getStatusCode());
                ssRoleLinkTable.setSsEdit(StatusWhetherEnum.STATUS_Y.getStatusCode());
                ssRoleLinkTable.setSsTableEdit(StatusWhetherEnum.STATUS_Y.getStatusCode());
                ssRoleLinkTable.setSsCreateDate(DUtil.splitNow()[0]);
                ssRoleLinkTable.setSsCreateTime(DUtil.splitNow()[1]);
                ssRoleLinkTable.setSsCreateUser(MdmConstant.DEFAULT_MDM_USER);
                if (CollUtil.isEmpty(this.list(Wrappers.lambdaQuery(SsRoleLinkTable.class).eq(SsRoleLinkTable::getSsRole, role).eq(SsRoleLinkTable::getSsTable, tableVO.getTableCode())))) {
                    this.save(ssRoleLinkTable);
                }
                List<String> column = businessDatabaseHelper.getColumn(tableVO.getTableCode()).stream().map(DictFieldVO::getFieldCode).collect(Collectors.toList());
                List<SsRoleLinkField> roleLinkFields = CollUtil.newArrayList();
                for (String col : column) {
                    SsRoleLinkField ssRoleLinkField = new SsRoleLinkField();
                    ssRoleLinkField.setSsTable(tableVO.getTableCode());
                    ssRoleLinkField.setSsField(col);
                    ssRoleLinkField.setSsRole(role);
                    ssRoleLinkField.setSsBrowse(StatusWhetherEnum.STATUS_Y.getStatusCode());
                    ssRoleLinkField.setSsEdit(StatusWhetherEnum.STATUS_Y.getStatusCode());
                    ssRoleLinkField.setSsCreateDate(DUtil.splitNow()[0]);
                    ssRoleLinkField.setSsCreateTime(DUtil.splitNow()[1]);
                    ssRoleLinkField.setSsCreateUser(MdmConstant.DEFAULT_MDM_USER);
                    if (CollUtil.isEmpty(roleLinkFieldService
                            .list(Wrappers.lambdaQuery(SsRoleLinkField.class)
                                    .eq(SsRoleLinkField::getSsRole, role).eq(SsRoleLinkField::getSsTable, tableVO.getTableCode()).eq(SsRoleLinkField::getSsField, col)))) {
                        roleLinkFields.add(ssRoleLinkField);
                    }
                }
                if (roleLinkFields.size() > 0) {
                    roleLinkFieldService.saveBatch(roleLinkFields);
                }
            }
        }
    }
}
